package com.example.common.view.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.common.view.model.BaseRepository
import com.example.common.view.model.IResultData
import com.google.gson.Gson
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.newFixedThreadPoolContext
import kotlinx.coroutines.newSingleThreadContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.concurrent.Executors


/**
 * 基础ViewModel层
 * ViewModel和AndroidViewModel的主页区别在于是否需要上下文对象
 * ViewModel中业务:
 *  1.使用协程获取网络请求
 *  2.使用LiveData或StateFlow来管理数据
 *  3.通知View数据变化
 *
 *  注意:以前使用LiveData来缓存数据,存在数据倒灌问题,官方推荐在Kotlin中使用Flow流,如果是Java中官方推荐SingerLiveEvent或unPeekLiveData
 */
abstract class BaseViewModel:ViewModel() {
    //定义LiveData缓存数据
    val mError = MutableLiveData<String>()
    //统一协程异常处理
    private val mExceptionHandler = CoroutineExceptionHandler{ _,exception->
        Log.e("Coroutine Error","Exception ===> \n"+ exception.message)
        mError.postValue("ViewModel层发生异常:"+exception.message)
    }
    /**
     * 自定义协程作用域,SupervisorJob()创建独立协程作用域,避免影响父协程和同级协程
     */
    protected val mViewModelScope = CoroutineScope(Dispatchers.Main + SupervisorJob() + mExceptionHandler)
    //自定义协程调度器
    protected val mDispatcher = Executors.newFixedThreadPool(10).asCoroutineDispatcher()
    protected val mSingeDispatcher = newSingleThreadContext("SingleThreadDispatcher")
    protected val mFixedThreadDispatcher = newFixedThreadPoolContext(16,"SingleThreadDispatcher")
}